package com.jerei.crm.controller;

import com.jerei.crm.entity.Contract;
import com.jerei.crm.entity.Customer;
import com.jerei.crm.mapper.CustomerMapper;
import com.jerei.crm.service.ICustomerService;
import com.jerei.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.nio.ch.Net;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    private ICustomerService customerService;



    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }



    @RequestMapping("/add")
    public JsonResult<Void> addCustomer(Customer customer, HttpSession session){

        System.out.println("信息："+customer);
        Integer uid=getUidFromSession(session);
        customer.setUid(uid);
        customerService.addCus(customer);

        return new JsonResult<>(OK);
    }

@RequestMapping("/like")
public JsonResult<List<Customer>> find(HttpSession session,String name,String tel,String intention,Integer uid ){
        if(uid==null){
            uid=getUidFromSession(session);
        }
        String username=getUsernameFromSession(session);
    System.out.println("name="+name+",tel="+tel+",intention="+intention+",uid="+uid);
        List<Customer> list=customerService.findLike(uid,name,tel,intention,username);
    System.out.println("模糊查询结果="+list);
        return new JsonResult<>(OK,list);

}


@RequestMapping("/likeMyCus")
public JsonResult<List<Customer>> findMyCus(HttpSession session,String name,String tel,String intention,Integer uid ){
        if(uid==null){
            uid=getUidFromSession(session);
        }
        String username=getUsernameFromSession(session);
        System.out.println("name="+name+",tel="+tel+",intention="+intention+",uid="+uid);
        List<Customer> list=customerService.findLikeMyCus(uid,name,tel,intention,username);
        System.out.println("正式客户的模糊查询结果="+list);
        return new JsonResult<>(OK,list);
}

    @RequestMapping("/likeAll")
    public JsonResult<List<Customer>> findAll(HttpSession session,String name,String tel,String intention,Integer uid ){
        String username=getUsernameFromSession(session);
        System.out.println("name="+name+",tel="+tel+",intention="+intention+",uid="+uid);
        List<Customer> list=customerService.findLikeAll(uid,name,tel,intention,username);
        System.out.println("正式客户的模糊查询结果="+list);
        return new JsonResult<>(OK,list);
    }


@RequestMapping("/likePublic")
public JsonResult<List<Customer>> findPublic(String name,String tel,String intention){
        List<Customer> list=customerService.findLikePublic(name, tel, intention);
        return new JsonResult<>(OK,list);
}


//通过uid获取该账户下的所有信息
@RequestMapping("/showAll")
public  JsonResult<List<Customer>> findAllByUid(HttpSession session,Integer index){
        Integer uid=getUidFromSession(session);
        List<Customer> list=customerService.findAllByUid(uid,index);
        return new JsonResult<>(OK,list);

}

    @RequestMapping("/showStatus")
    public  JsonResult<List<Customer>> findAllByStatus(HttpSession session,Integer index){
        Integer uid=getUidFromSession(session);
        List<Customer> list=customerService.findAllByStatus(uid,index);
        return new JsonResult<>(OK,list);

    }

@RequestMapping("/showPublic")
    public  JsonResult<List<Customer>> findAllByPublic(Integer index){
        List<Customer> list=customerService.findAllByPublic(index);
        return new JsonResult<>(OK,list);

    }

//展示所有的不局限于uid
@RequestMapping("/All")
public JsonResult<List<Customer>> showAll(HttpSession session,Integer index){
        String username=getUsernameFromSession(session);
        List<Customer> list= customerService.findAll(username,index);
        return new JsonResult<>(OK,list);
}

@RequestMapping("/countAll")
public JsonResult<Integer> countAll(){

        Integer num=customerService.countAll();
        System.out.println("全部用户的数量="+num);
        return new JsonResult<>(OK,num);
    }

    @RequestMapping("/countPublic")
    public JsonResult<Integer> countPublic(){

        Integer num=customerService.countPublic();

        return new JsonResult<>(OK,num);
    }



@RequestMapping("/count")
public JsonResult<Integer> countCus(HttpSession session){
    Integer uid=getUidFromSession(session);
    Integer num=customerService.countCus(uid);
    System.out.println("uid用户的数量="+num);
    return new JsonResult<>(OK,num);
}

    @RequestMapping("/countStatus")
    public JsonResult<Integer> countStatus(HttpSession session){
        Integer uid=getUidFromSession(session);
        Integer num=customerService.countByStatus(uid);
        return new JsonResult<>(OK,num);
    }



//添加保险合同
@RequestMapping("/addZz")
public JsonResult<Void> addContract(Integer cid,Contract contract){
    System.out.println("cid="+cid);
    System.out.println(contract);
        contract.setCid(cid);
        customerService.addContract(contract);
        return new JsonResult<>(OK);
}

@RequestMapping("/addPublic")
public JsonResult<Void> addPublic(Integer cid){
        customerService.updateAbaPublic(cid);
        return new JsonResult<>(OK);
}



//通过cid查询保险
 @RequestMapping("/find/contract")
public JsonResult<List<Contract>> findContractByCid(Integer cid){
        List<Contract> list=customerService.findContractByCid(cid);
     System.out.println("list="+list);
        return new JsonResult<>(OK,list);

}

//转让显示
@RequestMapping("/arr")
public JsonResult<List<Customer>> findByArrayCid(Integer[] arr){
//    System.out.println("arr="+Arrays.toString(arr));
        List<Customer> list=customerService.findCustomerByArray(arr);
        return new JsonResult<>(OK,list);
}

@RequestMapping("/upByArrayCid")
public JsonResult<Void> updateByArrayCid(Integer[] arr,Integer uid){
    System.out.println("转让的cid="+Arrays.toString(arr)+",uid="+uid);
        customerService.updateByArrayCid(arr, uid);
        return new JsonResult<>(OK);
}

@RequestMapping("/updatePublic")
public JsonResult<Void> updatePublic(Integer cid,HttpSession session){

        Integer uid=getUidFromSession(session);
        customerService.updatePublic(uid,cid);
        return new JsonResult<>(OK);
}

@RequestMapping("/delete")
public JsonResult<Void> deleteByCid(Integer cid){
        customerService.deleteByCid(cid);
        return new JsonResult<>(OK);
}



}
